﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class LocalSupplierCfgForm : Form
    {
        private DataTable m_BindDT = null;
        public LocalSupplierCfgForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SupplierForm form = new SupplierForm();
            form.IsNew = true;
            if (DialogResult.OK == form.ShowDialog())
            {
                string code = form.Code;
                string supplier = form.SupplierName;

                string sql = "insert into Supplier (Code, Name, IsSystem) values ('"+ code + "','" + supplier + "'," + false + ")";
                try
                {
                    if (!CommonHelper.DoOpreation(sql))
                    {
                        MessageBox.Show("执行SQL：" + sql + "错误");
                        return;
                    }
                    MessageBox.Show("添加供应商成功！");
                }
                catch (Exception ex)
                {
                    string str = "添加供应商失败：" + ex.Message;
                    LogHelper.error(str);
                    MessageBox.Show(str);
                }
            }
            Refresh();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataGridView.Rows.Count == 0)
                return;
            if (dataGridView.CurrentRow.Index < 0)
            {
                MessageBox.Show("请选择需要删除的供应商！");
                return;
            }
            bool isSystem = dataGridView.CurrentRow.Cells["IsSystemColumn"].Value.ToString().ToUpper()
                == "TRUE" ? true : false;
            if (isSystem)
            {
                MessageBox.Show("该供应商为系统供应商不能删除，请重新选择！");
                return;
            }
            if (MessageBox.Show("确定删除该供应商？", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string code = dataGridView.CurrentRow.Cells["CodeColumn"].Value.ToString();
                string sql = "delete from Supplier where Code = '" + code + "'";
                try
                {
                    if (!CommonHelper.DoOpreation(sql))
                    {
                        MessageBox.Show("执行SQL：" + sql + "错误");
                        return;
                    }
                    MessageBox.Show("删除供应商成功！");
                }
                catch (Exception ex)
                {
                    string str = "删除供应商失败：" + ex.Message;
                    LogHelper.error(str);
                    MessageBox.Show(str);
                }
            }
            Refresh();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
             if (dataGridView.Rows.Count == 0)
                return;
            if (dataGridView.CurrentRow.Index < 0)
            {
                MessageBox.Show("请选择需要修改的供应商！");
                return;
            }
            bool isSystem = dataGridView.CurrentRow.Cells["IsSystemColumn"].Value.ToString().ToUpper()
                == "TRUE" ? true : false;
            if (isSystem)
            {
                MessageBox.Show("该供应商为系统供应商不能修改，请重新选择！");
                return;
            }
            string code = dataGridView.CurrentRow.Cells["CodeColumn"].Value.ToString();
            string supplierName = dataGridView.CurrentRow.Cells["SupplierColumn"].Value.ToString();

            SupplierForm form = new SupplierForm();
            form.IsNew = false;
            form.Code = code;
            form.SupplierName = supplierName;
            if (DialogResult.OK == form.ShowDialog())
            {
                string supplier = form.SupplierName;

                string sql = "update Supplier set Name = '" + supplier + "' where Code = '" + code + "'";
                try
                {
                    if (!CommonHelper.DoOpreation(sql))
                    {
                        MessageBox.Show("执行SQL：" + sql + "错误");
                        return;
                    }
                    MessageBox.Show("修改供应商成功！");
                }
                catch (Exception ex)
                {
                    string str = "修改供应商失败：" + ex.Message;
                    LogHelper.error(str);
                    MessageBox.Show(str);
                }
            }
            Refresh();
        }

        private void LocalSupplierCfgForm_Load(object sender, EventArgs e)
        {
            InitDT();
            Refresh();
        }

        private void InitDT()
        {
            m_BindDT = new DataTable();
            m_BindDT.Columns.Add("Code");
            m_BindDT.Columns.Add("Supplier");
            m_BindDT.Columns.Add("IsSystem");
        }

        private void Refresh()
        {
            m_BindDT.Rows.Clear();
            dataGridView.DataSource = m_BindDT;

            string sql = "select * from Supplier";
            try
            {
                DataTable dt = CommonHelper.DoSelectOpreation(sql);
                if (dt == null || dt.Rows.Count == 0)
                    return;
                foreach (DataRow dr in dt.Rows)
                    m_BindDT.Rows.Add(dr["Code"].ToString(), dr["Name"].ToString(), dr["IsSystem"]);
                dataGridView.DataSource = m_BindDT;
            }
            catch (Exception ex)
            {
                string str = "刷新供应商列表失败：" + ex.Message;
                LogHelper.error(str);
                MessageBox.Show(str);
            }
        }

        private void btnSynchronize_Click(object sender, EventArgs e)
        {
            if (!SynchronizeHelper.SynchronizeSupplier())
            {
                MessageBox.Show("同步系统供应商异常，详细信息请参考日志文件");
                return;
            }
            MessageBox.Show("同步系统供应商成功！");
            Refresh();
        }

    }
}
